package com.cskaoyan.mall.controller.admin;

import com.cskaoyan.mall.bean.Brand;
import com.cskaoyan.mall.bean.bo.BrandCreateBO;
import com.cskaoyan.mall.bean.bo.BrandListBO;
import com.cskaoyan.mall.bean.bo.BrandUpdateBO;
import com.cskaoyan.mall.bean.vo.GeneralItemsBean;
import com.cskaoyan.mall.bean.vo.ResultVO;
import com.cskaoyan.mall.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @Author: dyj
 * @Date: 2021/1/9 17:05
 * @Description  商场管理  品牌制造商模块
 */
@RestController
@RequestMapping("/admin/brand")
public class BrandController {

    @Autowired
    BrandService brandService;

    @RequestMapping("list")
    public ResultVO list(BrandListBO brandListBO){
        GeneralItemsBean<Brand> brandListVO = brandService.queryList(brandListBO);
        return ResultVO.ok(brandListVO);
    }


    @RequestMapping("create")
    public ResultVO create(@RequestBody BrandCreateBO brandCreateBO){

        // 参数校验
        try{
            new BigDecimal(brandCreateBO.getFloorPrice());
        } catch (Exception e){
            e.printStackTrace();
            return ResultVO.error(402,"参数值不对");
        }

        Brand brand = new Brand(brandCreateBO);
        brandService.insertBrand(brand);
        return ResultVO.ok(brand);
    }

    @RequestMapping("update")
    public ResultVO update(@RequestBody BrandUpdateBO brandUpdateBO){

        // 参数校验
        try{
            new BigDecimal(brandUpdateBO.getFloorPrice());
        } catch (Exception e){
            e.printStackTrace();
            return ResultVO.error(402,"参数值不对");
        }
        Brand brand = brandService.updateBrand(brandUpdateBO);
        return ResultVO.ok(brand);
    }

    @RequestMapping("delete")
    public ResultVO delete(@RequestBody Brand brand){
        brandService.deleteBrand(brand);
        return ResultVO.ok();
    }

}
